﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
namespace CommonUtil
{
    public enum DefaultColor
    {
        White,
        Black
    }
    public class DataTypeTrans
    {
        #region Object转Int
        /// <summary>
        /// Object转Int
        /// </summary>
        /// <param name="_Source"></param>
        /// <returns></returns>
        public static int ObjToInt(object _Source)
        {
            int _result = 0;
            try
            {
                if (ObjIsNotNull(_Source))
                {
                    _result = Convert.ToInt32(_Source);
                }
            }
            catch (System.Exception ex)
            {
                Logger.DefaultLog(ex.ToString(), LogType.Error);
                throw (ex);
            }
            return _result;
        }
        #endregion

        #region Object转string
        /// <summary>
        /// Object转string
        /// </summary>
        /// <param name="_Source"></param>
        /// <returns></returns>
        public static string ObjToStr(object _Source)
        {
            string _result = string.Empty;
            try
            {
                if (ObjIsNotNull(_Source))
                {
                    _result = _Source.ToString();
                }
            }
            catch (System.Exception ex)
            {
                Logger.DefaultLog(ex.ToString(), LogType.Error);
                throw (ex);
            }
            return _result;
        }
        #endregion

        #region Object转Bool
        /// <summary>
        /// Object转Bool
        /// </summary>
        /// <param name="_Source"></param>
        /// <returns></returns>
        public static bool ObjTobool(object _Source)
        {
            bool _result = false;
            try
            {
                if (ObjIsNotNull(_Source))
                {
                    _result = Convert.ToBoolean(_Source);
                }
            }
            catch (System.Exception ex)
            {
                Logger.DefaultLog(ex.ToString(), LogType.Error);
                //throw (ex);
            }
            return _result;
        }
        #endregion

        #region String转Bool
        /// <summary>
        /// String转Bool
        /// </summary>
        /// <param name="_Source"></param>
        /// <returns></returns>
        public static bool StrToBool(string _Source)
        {
            bool _result = false;
            try
            {
                if (!string.IsNullOrEmpty(_Source))
                {
                    _result = Convert.ToBoolean(_Source);
                }
            }
            catch (System.Exception ex)
            {
                Logger.DefaultLog(ex.ToString(), LogType.Error);
                throw (ex);
            }
            return _result;
        }
        #endregion

        #region Object转decimal
        /// <summary>
        /// Object转decimal
        /// </summary>
        /// <param name="_Source"></param>
        /// <returns></returns>
        public static decimal ObjToDec(object _Source)
        {
            decimal _result = 0;
            try
            {
            if (ObjIsNotNull(_Source))
            {
                _result = Convert.ToDecimal(_Source);
            }
            }
            catch (System.Exception ex)
            {
                Logger.DefaultLog(ex.ToString(), LogType.Error);
                throw (ex);
            }
            return _result;
        }
        #endregion

        #region 判断Object是不是null如果是返回false
        /// <summary>
        /// 判断Object是不是null如果是返回false
        /// </summary>
        /// <param name="_Source"></param>
        /// <returns></returns>
        public static bool ObjIsNotNull(object _Source)
        {
            bool _result = false;
            if (_Source != null && _Source != DBNull.Value)
            {
                _result = true;
            }
            return _result;
        }
        #endregion

        #region String转DateTime
        /// <summary>
        ///  String转DateTime
        /// </summary>
        /// <param name="_Source"></param>
        /// <returns></returns>
        public static DateTime StrToDateTime(string _Source)
        {
            DateTime _result = DateTime.Now;
            try
            {
                if (!string.IsNullOrEmpty(_Source))
                {
                    _result = Convert.ToDateTime(_Source);
                }
            }
            catch (System.Exception ex)
            {
                Logger.DefaultLog(ex.ToString(), LogType.Error);
                throw (ex);
            }
            return _result;
        }
        #endregion

        #region String数组转Color
        /// <summary>
        ///  String数组转Color
        /// </summary>
        /// <param name="_Source"></param>
        /// <returns></returns>
        public static Color StrToColor(string _Source, DefaultColor _default)
        {
            Color _result = new Color();
            try
            {
                if (!string.IsNullOrEmpty(_Source))
                {
                    string[] _Color = _Source.Split(new char[1] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                    for (int i = 0; i < _Color.Length; i++)
                    {
                        if (Convert.ToInt32(_Color[i]) != 0)
                        {
                            _result = Color.FromArgb(Convert.ToInt32(_Color[0]), Convert.ToInt32(_Color[1]), Convert.ToInt32(_Color[2]));
                            break;
                        }
                    }
                }
                else
                {
                    switch (_default)
                    {
                        case DefaultColor.White:
                            {
                                _result = Color.FromArgb(255, 255, 255);
                                break;
                            }
                        case DefaultColor.Black:
                            {
                                _result = Color.FromArgb(0, 0, 0);
                                break;
                            }
                    }
                }            	
            }
            catch (System.Exception ex)
            {
                throw (ex);
            	Logger.DefaultLog(ex.ToString(), LogType.Error);
            }
            return _result;
        }
        #endregion

        #region 判断Object的原始类型，如果非int或者decimal，那么在值的两段加上“'”
        /// <summary>
        /// 判断Object的原始类型
        /// </summary>
        /// <param name="_obj"></param>
        /// <param name="_type"></param>
        /// <returns></returns>
        public static object CHeckobj(object _obj, Type _type)
        {
            if (_type != typeof(int) && _type != typeof(decimal))
            {
                _obj = "'" + _obj + "'";
            }
            else
            {
                if (_obj == DBNull.Value || string.IsNullOrEmpty(ObjToStr(_obj)))
                {
                    _obj = 0;
                }
            }
            return _obj;
        }
        /// <summary>
        /// 判断Object的原始类型
        /// </summary>
        /// <param name="_obj"></param>
        /// <param name="_type"></param>
        /// <returns></returns>
        public static object CHeckobj(object _obj)
        {
            if (_obj.GetType()!= typeof(int) && _obj.GetType() != typeof(decimal))
            {
                _obj = "'" + _obj + "'";
            }
            if (_obj == DBNull.Value || string.IsNullOrEmpty(ObjToStr(_obj)))
            {
                _obj = 0;
            }
            return _obj;
        }
        #endregion
    }
}
